home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / TELECOMM / PCCP047 / TERMPLAY.C < prev    next >
Text File  |  1992-08-18  |  14KB  |  688 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  */
  3.  
  4. #include<stdio.h>
  5. #include<bios.h>
  6. #include<dos.h>
  7. #include<fcntl.h>
  8. #include<graph.h>
  9. #include"emu.h"
  10. #include"color.h"
  11.  
  12. #define LISTSIZ 16
  13.  
  14. struct
  15.     {
  16.     short index;
  17.     short row;
  18.     char rowset;
  19.     short column;
  20.     char colset;
  21.     short list[LISTSIZ];
  22.     short listindex;
  23.     }
  24.     funcstor[NFUNCS];
  25.  
  26. clrfuncstor(seqn)
  27.     short seqn;
  28.     {
  29.     int i;
  30.     if(funcstor[seqn].listindex==LISTSIZ)
  31.         funcstor[seqn].listindex--;
  32.     for(i=funcstor[seqn].listindex;i>=0;i--)
  33.         funcstor[seqn].list[i]=0;
  34.     funcstor[seqn].row=funcstor[seqn].column=funcstor[seqn].listindex=funcstor[seqn].index=funcstor[seqn].rowset=funcstor[seqn].colset=0;
  35.     }
  36.  
  37. int bold, faint, blink, inverse, bkcolor, fgcolor;
  38. struct videoconfig far *vconf;
  39.  
  40. atthndl()
  41.     {
  42.     int adds, workingbk, workingfg;
  43.     adds=0;
  44.     if((vconf->adapter!=_MDPA)&&(vconf->adapter!=_HGC))
  45.         {
  46.         workingbk=bkcolor;
  47.         workingfg=fgcolor;
  48.         if(faint)
  49.             if(inverse)
  50.                 if(bkcolor==WHITE)
  51.                     workingbk=GRAY;
  52.                 else if(bkcolor==BLACK)
  53.                     ;
  54.                 else
  55.                     adds+=FAINTADD;
  56.             else
  57.                 if(fgcolor==WHITE)
  58.                     workingfg=GRAY;
  59.                 else if(fgcolor==BLACK)
  60.                     ;
  61.                 else
  62.                     adds+=FAINTADD;
  63.         else
  64.             if(bold)
  65.                 {
  66.                 if(bkcolor==WHITE)
  67.                     workingbk=BWHITE;
  68.                 if(fgcolor==WHITE)
  69.                     workingfg=BWHITE;
  70.                 }
  71.         if(blink)
  72.             adds+=BLINKADD;
  73.         if(inverse)
  74.             {
  75.             _settextcolor(workingbk+adds);
  76.             _setbkcolor((long)(workingfg&0x07));
  77.             }
  78.         else
  79.             {
  80.             _settextcolor(workingfg+adds);
  81.             _setbkcolor((long)(workingbk&0x07));
  82.             }
  83.         }
  84.     else
  85.         {
  86.         workingbk=BLACK;
  87.         if(bold&&!inverse)
  88.             workingfg=M_UNDER;
  89.         else
  90.             if(inverse)
  91.                 workingfg=WHITE;
  92.             else
  93.                 workingfg=M_NORMAL;
  94.         if(faint)
  95.             if(inverse)
  96.                 if(bkcolor!=BLACK)
  97.                     adds+=FAINTADD;
  98.                 else;
  99.             else
  100.                 if(fgcolor!=BLACK)
  101.                     adds+=FAINTADD;
  102.         if(blink)
  103.             adds+=BLINKADD;
  104.         if(inverse)
  105.             {
  106.             _settextcolor(workingbk+adds);
  107.             _setbkcolor((long)(workingfg&0x07));
  108.             }
  109.         else
  110.             {
  111.             _settextcolor(workingfg+adds);
  112.             _setbkcolor((long)(workingbk&0x07));
  113.             }
  114.         }
  115.     }
  116.  
  117.  
  118. ansiatthndl(seqn)
  119.     short seqn;
  120.     {
  121.     int i, adds, workingbk, workingfg;
  122.     if(emu.funcs[seqn].func==ANSIATTRIB)
  123.         for(i=0;i<funcstor[seqn].listindex;i++)
  124.             switch(funcstor[seqn].list[i])
  125.                 {
  126.                 /*at the moment this is strictly ANSI subset*/
  127.                 case 0:
  128.                     bkcolor=BLACK;
  129.                     fgcolor=WHITE;
  130.                     bold=faint=blink=inverse=0;
  131.                     break;
  132.                 case 1:
  133.                     bold=1;
  134.                     break;
  135.                 case 2:
  136.                     faint=1;
  137.                     break;
  138.                 case 5:
  139.                 case 6:
  140.                     blink=1;
  141.                     break;
  142.                 case 7:
  143.                     inverse=1;
  144.                     break;
  145.                 case 31:
  146.                 case 41:
  147.                     fgcolor=RED;
  148.                     break;
  149.                 case 32:
  150.                 case 42:
  151.                     fgcolor=GREEN;
  152.                     break;    
  153.                 case 33:
  154.                 case 43:
  155.                     fgcolor=YELLOW;
  156.                     break;
  157.                 case 34:
  158.                 case 44:
  159.                     fgcolor=BLUE;
  160.                     break;
  161.                 case 35:
  162.                 case 45:
  163.                     fgcolor=MAGENTA;
  164.                     break;
  165.                 case 36:
  166.                 case 46:
  167.                     fgcolor=CYAN;
  168.                     break;
  169.                 case 37:
  170.                 case 47:
  171.                     fgcolor=WHITE;
  172.                     break;
  173.                 default:
  174.                     break;
  175.                 }
  176.     atthndl();
  177.     }
  178.  
  179. int wrap_p;
  180.  
  181. wrapctl()
  182.     {
  183.     if(wrap_p)
  184.         _wrapon(_GWRAPON);
  185.     else
  186.         _wrapon(_GWRAPOFF);
  187.     }
  188.  
  189. char fpname[256];
  190.  
  191. updstatus(c)
  192.     {
  193.     struct rccoord posptr;
  194.     short tc;
  195.     long bc;
  196.     char str[80];
  197.     posptr=_gettextposition();
  198.     tc=_gettextcolor();
  199.     bc=_getbkcolor();
  200.     _settextwindow(1,1,1,80);
  201.     _settextposition(1,1);
  202.     _settextcolor(BLACK);
  203.     _setbkcolor((long)WHITE);
  204.     _wrapon(_GWRAPOFF);
  205.     sprintf(str, "Next=%02x  Bold=%d  Faint=%d  Blink=%d  Inverse=%d", c, bold, faint, blink, inverse);
  206.     _outtext(str);
  207.     _settextwindow(2,1,25,80);
  208.     wrapctl();
  209.     _settextcolor(tc);
  210.     _setbkcolor(bc);
  211.     _settextposition(posptr.row, posptr.col);
  212.     }
  213.  
  214. int graphics;
  215.  
  216. showchar(c)
  217.     unsigned char c;
  218.     {
  219.     unsigned char str[2];
  220.     if(graphics)
  221.         if(emu.gchars[c])
  222.             c=emu.gchars[c];
  223.     str[0]=c;
  224.     str[1]='\0';
  225.     _outtext(str);
  226.     }
  227.  
  228. int savedrow, savedcol;
  229.  
  230. perffunc(seqn)
  231.     short seqn;
  232.     {
  233.     struct rccoord posptr;
  234.     int i;
  235.     switch(emu.funcs[seqn].func)
  236.         {
  237.         case CLEAR:
  238.             _clearscreen(_GCLEARSCREEN);
  239.             updstatus();
  240.             _settextposition(1,1);
  241.             break;
  242.         case HOME:
  243.             _settextposition(1,1);
  244.             break;
  245.         case CLREOL:
  246.             posptr=_gettextposition();
  247.             _wrapon(_GWRAPOFF);
  248.             for(i=posptr.col;i<=80;i++)
  249.                 _outtext(" ");
  250.             _settextposition(posptr.row, posptr.col);
  251.             wrapctl();
  252.             break;
  253.         case UP:
  254.             posptr=_gettextposition();
  255.             if(posptr.row>1)
  256.                 _settextposition(posptr.row-1, posptr.col);
  257.             break;
  258.         case DOWN:
  259.             posptr=_gettextposition();
  260.             if(posptr.row<24)
  261.                 _settextposition(posptr.row+1, posptr.col);
  262.             else
  263.                 {
  264.                 showchar('\n');
  265.                 _settextposition(posptr.row, posptr.col);
  266.                 }
  267.             break;
  268.         case LEFT:
  269.             posptr=_gettextposition();
  270.             if(posptr.col>1)
  271.                 _settextposition(posptr.row, posptr.col-1);
  272.             break;
  273.         case RIGHT:
  274.             posptr=_gettextposition();
  275.             if(posptr.col<80)
  276.                 _settextposition(posptr.row, posptr.col+1);
  277.             break;
  278.         case GOTO:
  279.             funcstor[seqn].row-=(emu.firstrowaddr-1);
  280.             funcstor[seqn].column-=(emu.firstcoladdr-1);
  281.             if(funcstor[seqn].row<1)
  282.                 funcstor[seqn].row=1;
  283.             if(funcstor[seqn].row>24)
  284.                 funcstor[seqn].row=24;
  285.             if(funcstor[seqn].column<1)
  286.                 funcstor[seqn].column=1;
  287.             if(funcstor[seqn].column>80)
  288.                 funcstor[seqn].row=80;
  289.             _settextposition((emu.tophi_p?25-funcstor[seqn].row:funcstor[seqn].row), funcstor[seqn].column);
  290.             break;
  291.         case NORMAL:
  292.             fgcolor=WHITE;
  293.             bkcolor=BLACK;
  294.             blink=faint=bold=inverse=0;
  295.             atthndl();
  296.             break;
  297.         case BLINK:
  298.             blink=1;
  299.             atthndl();
  300.             break;
  301.         case NOBLINK:
  302.             blink=0;
  303.             atthndl();
  304.             break;
  305.         case BOLD:
  306.             bold=1;
  307.             atthndl();
  308.             break;
  309.         case NOBOLD:
  310.             bold=0;
  311.             atthndl();
  312.             break;
  313.         case FAINT:
  314.             faint=1;
  315.             atthndl();
  316.             break;
  317.         case NOFAINT:
  318.             faint=0;
  319.             atthndl();
  320.             break;
  321.         case INVERSE:
  322.             inverse=1;
  323.             atthndl();
  324.             break;
  325.         case NOINVERSE:
  326.             inverse=0;
  327.             atthndl();
  328.             break;
  329.         case UPN:
  330.             posptr=_gettextposition();
  331.             if(posptr.row-funcstor[seqn].row<1)
  332.                 _settextposition(1, posptr.col);
  333.             else
  334.                 _settextposition(posptr.row-funcstor[seqn].row, posptr.col);
  335.             break;
  336.         case DOWNN:
  337.             posptr=_gettextposition();
  338.             if(posptr.row+funcstor[seqn].row>24)
  339.                 {
  340.                 _settextposition(24,1);
  341.                 for(i=25-(posptr.row+funcstor[seqn].row);i<0;++i)
  342.                     showchar('\n');
  343.                 _settextposition(24, posptr.col);
  344.                 }
  345.             else
  346.                 _settextposition(posptr.row+funcstor[seqn].row, posptr.col);
  347.             break;
  348.         case LEFTN:
  349.             posptr=_gettextposition();
  350.             if(posptr.col-funcstor[seqn].column<1)
  351.                 _settextposition(posptr.row, 1);
  352.             else
  353.                 _settextposition(posptr.row, posptr.col-funcstor[seqn].column);
  354.             break;
  355.         case RIGHTN:
  356.             posptr=_gettextposition();
  357.             if(posptr.col+funcstor[seqn].column>80)
  358.                 _settextposition(posptr.row, 80);
  359.             else
  360.                 _settextposition(posptr.row, posptr.col+funcstor[seqn].column);
  361.             break;
  362.         case ANSIATTRIB:
  363.             ansiatthndl(seqn);
  364.             break;
  365.         case WRAP:
  366.             wrap_p=1;
  367.             wrapctl();
  368.             break;
  369.         case NOWRAP:
  370.             wrap_p=0;
  371.             wrapctl();
  372.             break;
  373.         case GOTOLINE:
  374.             funcstor[seqn].row-=(emu.firstrowaddr-1);
  375.             posptr=_gettextposition();
  376.             if(funcstor[seqn].row<1)
  377.                 funcstor[seqn].row=1;
  378.             if(funcstor[seqn].row>24)
  379.                 funcstor[seqn].row=24;
  380.             _settextposition(emu.tophi_p?25-funcstor[seqn].row:funcstor[seqn].row, 1);
  381.             break;
  382.         case GOTOCOL:
  383.             funcstor[seqn].column-=(emu.firstcoladdr-1);
  384.             posptr=_gettextposition();
  385.             if(funcstor[seqn].column<1)
  386.                 funcstor[seqn].column=1;
  387.             if(funcstor[seqn].column>80)
  388.                 funcstor[seqn].row=80;
  389.             _settextposition(posptr.row, funcstor[seqn].column);
  390.             break;
  391.         case BINATTR:
  392.             if((funcstor[seqn].row-emu.attroffset)&emu.blinkmask)
  393.                 blink=1;
  394.             else
  395.                 blink=0;
  396.             if((funcstor[seqn].row-emu.attroffset)&emu.inversemask)
  397.                 inverse=1;
  398.             else
  399.                 inverse=0;
  400.             if((funcstor[seqn].row-emu.attroffset)&emu.boldmask)
  401.                 bold=1;
  402.             else
  403.                 bold=0;
  404.             if((funcstor[seqn].row-emu.attroffset)&emu.faintmask)
  405.                 faint=1;
  406.             else
  407.                 faint=0;
  408.             atthndl();
  409.             break;
  410.         case GRAPHCHAR:
  411.             showchar(emu.gchars[funcstor[seqn].row]);
  412.             break;
  413.         case BEGGRAPH:
  414.             graphics=1;
  415.             break;
  416.         case ENDGRAPH:
  417.             graphics=0;
  418.             break;
  419.         case TAB:
  420.             posptr=_gettextposition();
  421.             _settextposition(posptr.row, posptr.col+(8-((posptr.col-1)%8)));
  422.             break;
  423.         case BELL:
  424.             putch(7);
  425.             break;
  426.         case DTAB:
  427.             posptr=_gettextposition();
  428.             for(i=0;i<(8-((posptr.col-1)%8));++i)
  429.                 _outtext(" ");
  430.  
  431.         case CRLF:
  432.             showchar('\n');
  433.             break;
  434.         case SAVEPOS:
  435.             posptr=_gettextposition();
  436.             savedrow=posptr.row;
  437.             savedcol=posptr.col;
  438.             break;
  439.         case RESTOREPOS:
  440.             _settextposition(savedrow, savedcol);
  441.             break;
  442.         default:
  443.             break;
  444.         }
  445.     }
  446.  
  447. dispport(c)
  448.     unsigned char c;
  449.     {
  450.     int seqn, done, ok;
  451.     done=0;
  452.     ok=0;
  453.     for(seqn=0;seqn<NFUNCS;seqn++)
  454.         {
  455.         if(emu.funcs[seqn].codes[funcstor[seqn].index]&0xff00)
  456.             switch(emu.funcs[seqn].codes[funcstor[seqn].index])
  457.                 {
  458.                 case END:
  459.                     if(funcstor[seqn].index)
  460.                         {
  461.                         done=1;
  462.                         perffunc(seqn);
  463.                         }
  464.                     break;
  465.                 case BINCOL:
  466.                     funcstor[seqn].column=c-emu.bincoloff;
  467.                     ok=1;
  468.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  469.                         {
  470.                         done=1;
  471.                         perffunc(seqn);
  472.                         }
  473.                     break;
  474.                 case BINROW:
  475.                     funcstor[seqn].row=c-emu.binrowoff;
  476.                     ok=1;
  477.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  478.                         {
  479.                         done=1;
  480.                         perffunc(seqn);
  481.                         }
  482.                     break;
  483.                 case ASCDECCOL:
  484.                     if((c>='0')&&(c<='9'))
  485.                         {
  486.                         ok=1;
  487.                         funcstor[seqn].column*=10;
  488.                         funcstor[seqn].column+=(c-'0');
  489.                         funcstor[seqn].colset=1;
  490.                         }
  491.                     else
  492.                         if(funcstor[seqn].colset&&(c==emu.funcs[seqn].codes[++funcstor[seqn].index]))
  493.                             {
  494.                             ok=1;
  495.                             if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  496.                                 {
  497.                                 done=1;
  498.                                 perffunc(seqn);
  499.                                 }
  500.                             }
  501.                         else
  502.                             clrfuncstor(seqn);
  503.                     break;
  504.                 case ASCDECROW:
  505.                     if((c>='0')&&(c<='9'))
  506.                         {
  507.                         ok=1;
  508.                         funcstor[seqn].row*=10;
  509.                         funcstor[seqn].row+=(c-'0');
  510.                         funcstor[seqn].rowset=1;
  511.                         }
  512.                     else
  513.                         if(funcstor[seqn].rowset&&(c==emu.funcs[seqn].codes[++funcstor[seqn].index]))
  514.                             {
  515.                             ok=1;
  516.                             if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  517.                                 {
  518.                                 done=1;
  519.                                 perffunc(seqn);
  520.                                 }
  521.                             }
  522.                         else
  523.                             clrfuncstor(seqn);
  524.                     break;
  525.                 case ASCDECSEMILIST:
  526.                     if((c>='0')&&(c<='9')&&(funcstor[seqn].listindex<LISTSIZ))
  527.                         {
  528.                         ok=1;
  529.                         funcstor[seqn].list[funcstor[seqn].listindex]*=10;
  530.                         funcstor[seqn].list[funcstor[seqn].listindex]+=(c-'0');
  531.                         }
  532.                     else
  533.                         {
  534.                         if(funcstor[seqn].listindex<LISTSIZ)
  535.                             funcstor[seqn].listindex++;
  536.                         
  537.                         if(c==';')
  538.                             ok=1;
  539.                         else
  540.                             if(c==emu.funcs[seqn].codes[++funcstor[seqn].index])
  541.                                 {
  542.                                 ok=1;
  543.                                 if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  544.                                     {
  545.                                     done=1;
  546.                                     perffunc(seqn);
  547.                                     }
  548.                                 }
  549.                             else
  550.                                 clrfuncstor(seqn);
  551.                         }
  552.                     break;
  553.                 case GRABCHAR:
  554.                     ok=1;
  555.                     funcstor[seqn].row=c;
  556.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  557.                         {
  558.                         done=1;
  559.                         perffunc(seqn);
  560.                         }
  561.                     break;
  562.                 case GRAPHCHAR_T:
  563.                     if(emu.gchars[c])
  564.                         {
  565.                         ok=1;
  566.                         funcstor[seqn].row=c;
  567.                         if(emu.funcs[seqn].codes[funcstor[seqn].index]==END)
  568.                             {
  569.                             done=1;
  570.                             perffunc(seqn);
  571.                             }
  572.                         }
  573.                     else
  574.                         clrfuncstor(seqn);
  575.                     break;
  576.                 }
  577.         else
  578.             if(c==emu.funcs[seqn].codes[funcstor[seqn].index])
  579.                 {
  580.                 ok=1;
  581.                 if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  582.                     {
  583.                     done=1;
  584.                     perffunc(seqn);
  585.                     }
  586.                 }
  587.             else
  588.                 clrfuncstor(seqn);
  589.         if(done)
  590.             {
  591.             ok=1;
  592.             for(seqn=0;seqn<NFUNCS;seqn++)
  593.                 clrfuncstor(seqn);
  594.             break;
  595.             }
  596.         }
  597.     if(!ok)
  598.         showchar(c);
  599.     }
  600.  
  601. initdisp()
  602.     {
  603.     int i;
  604.     for(i=0;i<NFUNCS;i++)
  605.         {
  606.         funcstor[i].listindex=LISTSIZ;
  607.         clrfuncstor(i);
  608.         }
  609.     bold=faint=blink=inverse=0;
  610.     fgcolor=WHITE;
  611.     bkcolor=BLACK;
  612.     graphics=0;
  613.     wrap_p=emu.default_wrap_p;
  614.     }
  615.  
  616. main(argc, argv)
  617.     int argc;
  618.     char **argv;
  619.     {
  620.     FILE *playfd;
  621.     char c;
  622.     struct rccoord posptr;
  623.     int follow, emufd, i;
  624.     _getvideoconfig(vconf);
  625.     _setvideomode(_DEFAULTMODE);
  626.     if(argc!=3)
  627.         {
  628.         printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  629.         printf("USAGE: termplay <emu or - > <script>\n");
  630.         printf("<emu> is an emulation file base pathname.\n");
  631.         printf("<script> is a file of codes to play.\n");
  632.         printf("The environment variable PCCPPATH is used for the emulation file if set.\n");
  633.         exit(1);
  634.         }
  635.     fpname[0]='\0';
  636.     if(argv[1][0]!='-')
  637.         {
  638.         if((getenv("PCCPPATH")==NULL)||(argv[1][0]=='\\')||(argv[1][1]==':'))
  639.             sprintf(fpname, "%s.emu", argv[1]);
  640.         else
  641.             sprintf(fpname, "%s\\%s.emu", getenv("PCCPPATH"), argv[1]);
  642.         if((emufd=open(fpname, O_RDONLY|O_BINARY))==-1)
  643.             {
  644.             printf("Error opening emulation file %s.\n", fpname);
  645.             exit(2);
  646.             }
  647.         else
  648.             if(read(emufd, &emu, sizeof(emu))!=sizeof(emu))
  649.                 {
  650.                 printf("Error reading emulation file %s.\n", fpname);
  651.                 exit(3);
  652.                 }
  653.             else;
  654.         }
  655.     else
  656.         {
  657.         nullemu();
  658.         emu.funcs[0].func=LEFT;
  659.         emu.funcs[0].codes[0]='\b';
  660.         emu.funcs[0].codes[1]=END;
  661.         emu.funcs[1].func=DTAB;
  662.         emu.funcs[1].codes[0]='\t';
  663.         emu.funcs[1].codes[1]=END;
  664.         emu.funcs[2].func=BELL;
  665.         emu.funcs[2].codes[0]='\007';
  666.         emu.funcs[2].codes[1]=END;
  667.         }
  668.     if((playfd=fopen(argv[2], "r"))==NULL)
  669.         {
  670.         printf("Couldn't open script file %s.\n");
  671.         exit(10);
  672.         }
  673.     setmode(fileno(playfd), O_BINARY);
  674.     _clearscreen(_GCLEARSCREEN);
  675.     initdisp();
  676.     atthndl();
  677.     _settextposition(1,1);
  678.     if(vconf->adapter==_VGA)
  679.         _remappalette(GRAY, (long)0x00202020); /* Lighten up gray */
  680.     while(1)
  681.         {
  682.         c=getc(playfd);
  683.         updstatus(c);
  684.         getch();
  685.         dispport(c);
  686.         }
  687.     }
  688.